package TUILag;
import CtrLag.*;
import ModelLag.*;

/**
 * 
 * 
 * @author (Lackshan, Mikkel, Nick, patrick, Simon, Yousef) 
 * @project (Tema design)
 * @version (2014.11.04)
 */
public class PersonUI
{
    private PersonCtr pCtr = new PersonCtr();
    private InputReader reader = new InputReader();
    
    /**
     * Constructor for objects of class PersonUI
     */
    public PersonUI(){

    }

    /**
     * An example of a method - replace this comment with your own
     * 
     * @param  y   a sample parameter for a method
     * @return     the sum of x and y 
     */
    public void run(){
        printMenu();
        boolean finished = false;
        while(!finished) {
            String choise = reader.getInput();
            switch(choise){
                case "1":
                    addNewFriend();
                    break;
                case "2":
                   updateFriend();
                    break;
                case "3":
                   deleteFriend();
                    break;
                case "4":
                    finished = true;
                    break;
                default:
                    System.out.println("Not a command");
                    break;
            }
            printMenu();
        }
    }
    public void printMenu(){
        System.out.println("** Friends **");
        System.out.println("1. Add new friend");
        System.out.println("2. Update friend");
        System.out.println("3. Delete friend");
        System.out.println("4. Main menu");
    }
    private boolean addNewFriend(){
        String name = reader.getInputWithLabel("Name");
        String address = reader.getInputWithLabel("Address");
        String zip = reader.getInputWithLabel("Zip");
        String city = reader.getInputWithLabel("City");
        String phone = reader.getInputWithLabel("phone");
        createPerson(name, address, zip, city, phone);
        return true;
    }
    private void updateFriend(){
        String phone = reader.getInputWithLabel("phone");
        try{
            Person p = pCtr.getPerson(phone);
            System.out.println("Edit: " + p.getName());
            String newName = reader.getInputWithLabel("name");
            System.out.println(p.getName());
            String newAddress = reader.getInputWithLabel("Address");
            String newZip = reader.getInputWithLabel("Zip");
            String newCity = reader.getInputWithLabel("City");
            String newPhone = reader.getInputWithLabel("phone");
            updatePerson(p, newName, newAddress, newZip, newCity,newPhone); 
        }catch(Exception e){
            System.out.println("Person not found!");
        }
    }
    private void deleteFriend(){
        String phone = reader.getInputWithLabel("phone");

        try{
            Person p = pCtr.getPerson(phone);
            deletePerson(phone);
            System.out.println("Friend was deleted!");
        }catch(Exception e){
            System.out.println("Person not found!");
        }
    }
    private void createPerson(String name, String adress, String zip, String city, String phone){
        pCtr.createPerson(name, adress, zip, city, phone);
    }
    private void updatePerson(Person p, String newName, String newAdress, String newZip, String newCity, String newPhone){
        pCtr.updatePerson(p, newName, newAdress, newZip, newCity, newPhone);
    }
    private Person findPerson(String phone){
        return pCtr.getPerson(phone);
    }
    private void deletePerson(String phone){
        pCtr.deletePerson(phone);
    }

}
